#ifndef TSERVER_H
#define TSERVER_H

#include <QObject>
#include <list>
#include <mutex>

#include <Session.h>

class QWebSocketServer;
class QWebSocket;
class ClientManager;

class TServer : public QObject
{
    Q_OBJECT

private:
    TServer(QObject *parent = nullptr);
public:
    ~TServer();
public:
    // listeners : 
    void onNewConnection();
    //void onTextMessageRecieved(QWebSocket *client, const QString & message);
public:
    // internal getter/setter
    //void _onMainClient(QWebSocket * client);
public:
    QWebSocketServer *server = nullptr;
    ClientManager *clientManager = nullptr;

private:
    // inetnal listeners

    std::mutex packagesMutex;
    std::list<std::pair<QWebSocket *, QString>> packages;

    friend void Session::_send(const QString &text) const;
    void sendHelper(QWebSocket *client, const QString &text);
    void on_sendTimerTimeout();
private:
    // internal variables
    QTimer sendHelperTimer;

public:
    // public static methods:
    static TServer *instance();
private:
    static TServer *_instance;
};

#endif // TSERVER_H
